package com.github.pagehelper.filter;

import com.github.pagehelper.filter.entity.AbstractCondition;
import com.github.pagehelper.filter.entity.ArrayCondition;
import com.github.pagehelper.filter.entity.TextCondition;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class Filter {

    private Map<String, Object> originalParams;
    private List<AbstractCondition> conditions = new ArrayList<AbstractCondition>();

    public Filter(Map<String, Object> params) {
        this.originalParams = params;
        parse();
    }

    private void parse() {
        for(String key : originalParams.keySet()) {
            Object val = originalParams.get(key);
            if (isArray(val)) {
                conditions.add(new ArrayCondition(key, val));
            } else {
                conditions.add(new TextCondition(key, val));
            }
        }
    }

    private boolean isArray(Object obj) {
        String regex = "^\\[.+?\\]$";
        boolean isMatches = Pattern.matches(regex, obj.toString());
        return isMatches;
    }

    public List<AbstractCondition> getConditions() {
        return conditions;
    }

}
